package com.jian.sys.controller;

import cn.hutool.core.util.IdUtil;
import com.jian.sys.common.DataGridView;
import com.jian.sys.common.ResultObj;
import com.jian.sys.common.WebUtils;
import com.jian.sys.pojo.User;
import com.jian.sys.service.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/userOperate")
public class UserPeopleController {
    @Autowired
    UserService userService;

    /**
     * 获取当前用户的信息
     * @return
     */
    @RequestMapping("/loadUserInfo")
    public DataGridView loadUserInfo(){
        User user= (User) WebUtils.getSession().getAttribute("user");
        return new DataGridView(user);
    }
    /**
     * 修改密码
     * @return
     */
    @RequestMapping("/changePwd")
    public ResultObj changePwd(String pwd){
        try{
            User user= (User) WebUtils.getSession().getAttribute("user");
            String salt= IdUtil.simpleUUID();
            user.setSalt(salt);
            user.setPwd(new Md5Hash(pwd,salt,2).toString());
            userService.updateById(user);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 注销
     */
    @RequestMapping("/logoutSystem")
    public String logoutSystem(){
        HttpSession session = WebUtils.getSession();
        session.invalidate();
        return  "system/index/login";
    }
}
